package com.gl.java.sync;

import java.lang.ref.WeakReference;
import java.util.concurrent.locks.StampedLock;

public class ThreadLocalDemo {

    public static class MyRunnable implements Runnable {

        private final ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
        WeakReference<ThreadLocal<Integer>> cacheRef = new WeakReference<>(threadLocal);

        @Override
        public void run() {
            threadLocal.set((int) (Math.random() * 100D));
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {

            }
            System.out.println(threadLocal.get());
        }
    }

    public static void main(String[] args) {
        MyRunnable sharedRunnableInstance = new MyRunnable();
        Thread thread1 = new Thread(sharedRunnableInstance);
        Thread thread2 = new Thread(sharedRunnableInstance);
        thread1.start();
        thread2.start();
    }
}